package com.pbs.ui.dialog;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.pbs.net.WSF;
import com.pbs.ui.R;
import com.pbs.utils.EmailParser;

public class CreateAccountDialog extends Dialog implements OnClickListener {

	Handler myHandler;

	public CreateAccountDialog(Context context) {
		super(context, R.style.Dialog);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.create_account_dialog, null);
		((Button) layout.findViewById(R.id.submitButton))
				.setOnClickListener(this);
		((Button) layout.findViewById(R.id.cancelButton))
				.setOnClickListener(this);
		myHandler = new Handler();
		setContentView(layout);
	}
	
	public boolean checkValidAccount(String user, String pass, String cofirmPass, String email) {
		if (user == null || user.equals("")) {
			Toast.makeText(getContext(), R.string.msg_MissUsername, Toast.LENGTH_SHORT).show();
			return false;
		}
		if (pass == null || pass.equals("")) {
			Toast.makeText(getContext(), R.string.msg_MissPassword, Toast.LENGTH_SHORT).show();
			return false;
		}
		if (cofirmPass == null || cofirmPass.equals("") || !pass.equals(cofirmPass)) {
			Toast.makeText(getContext(), R.string.msg_WrongConfirmPassword, Toast.LENGTH_SHORT).show();
			return false;
		}
		if (email == null || email.equals("")) {
			Toast.makeText(getContext(), R.string.msg_MissEmail, Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!new EmailParser(email).isValid()) {
			Toast.makeText(getContext(), R.string.msg_EmailInvalid, Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
	
	public ProgressDialog showProgressDialog(String msg, boolean cancelable) {
		ProgressDialog pd = new ProgressDialog(getContext());
		if (msg != null)
			pd.setMessage(msg);
		pd.setIcon(R.drawable.process_icon);
		pd.setCancelable(cancelable);
		pd.show();
		return pd;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submitButton: {
			
			final String username = ((EditText) findViewById(R.id.txtUsername)).getText().toString();
			final String password = ((EditText) findViewById(R.id.txtPassword)).getText().toString();
			final String confirm_password = ((EditText) findViewById(R.id.txtConfirmPassword)).getText().toString();
			final String email = ((EditText) findViewById(R.id.txtEmail)).getText().toString();
			if (checkValidAccount(username, password, confirm_password, email)) {
				final ProgressDialog pd = showProgressDialog("Please waiting...", false);
				new Thread() {
					public void run() {
						try {
							final String msg = WSF.createAccount(username,
									password, email);
							myHandler.post(new Runnable() {
								@Override
								public void run() {
									pd.dismiss();
									Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
								}
							});
						} catch (Exception e) {
							myHandler.post(new Runnable() {
								@Override
								public void run() {
									pd.dismiss();
									Toast.makeText(getContext(), "Error when attempting connect to server", Toast.LENGTH_SHORT).show();
								}
							});
						}finally{
							dismiss();
						}
					}
				}.start();
			}
			
			break;
		}
		case R.id.cancelButton: {
			dismiss();
		}
		}
	}
}
